package com.eee.xiang.market.controller;

import com.eee.xiang.market.beans.OperateResponse;
import com.eee.xiang.market.beans.TCargo;
import com.eee.xiang.market.beans.TNoticeExample;
import com.eee.xiang.market.beans.TUser;
import com.eee.xiang.market.common.ResponseResult;
import com.eee.xiang.market.common.TableData;
import com.eee.xiang.market.dao.mapper.TNoticeMapper;
import com.eee.xiang.market.dao.mapper.TUserMapper;
import com.eee.xiang.market.service.IAdminService;
import com.eee.xiang.market.service.ICargoService;
import com.eee.xiang.market.service.IEmployeeService;
import com.sun.org.apache.xpath.internal.axes.HasPositionalPredChecker;
import com.sun.xml.internal.ws.resources.HttpserverMessages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

@Controller
@RequestMapping(value = "/admin")
public class AdminManagerController {

    @Autowired
    private HttpSession session;

    @Autowired
    private IAdminService adminService;

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private TUserMapper userMapper;

    @Autowired
    private TNoticeMapper noticeMapper;

    @Autowired
    private ICargoService cargoService;
    /**
     * 管理员主界面
     * @return
     */
    @RequestMapping(value = "/adminManager")
    String adminManager(ModelMap map){
        if(session.getAttribute("userInfo") == null){
            return "index";
        }
        map.addAttribute("userInfo", session.getAttribute("userInfo"));
        return "adminManager/adminManager";
    }

    /**
     * 人员管理界面
     * @return
     */
    @RequestMapping(value = "/userManager")
    String userManager(){
        return "adminManager/userManager";
    }

    @RequestMapping(value = "/signOut")
    String signOut(){
        session.invalidate();
        return "redirect:/login/index";
    }

    @RequestMapping(value = "/userInfo")
    String userInfo(ModelMap map){
        if(session.getAttribute("userInfo") == null){
            return "index";
        }
        map.addAttribute("userInfo", session.getAttribute("userInfo"));
        return "component/userInfo";
    }

    @RequestMapping(value = "/changePWD")
    @ResponseBody
    public OperateResponse changePWD(TUser user){
        OperateResponse operateResponse = adminService.changePWD(user);
        if(operateResponse.getStatus() == ResponseResult.SUCCESS ){
            session.setAttribute("userInfo",userMapper.selectByPrimaryKey(user.getUserId()));
        }
        return operateResponse;
    }

    @RequestMapping("/userList")
    String userList(){
        return "userManager/userList";
    }

    @RequestMapping(value = "/userListContent")
    String userList(ModelMap modelMap, TUser userForm, @PageableDefault(page = 1, size = 15) Pageable pageable) {
        List<TUser> tUsers = adminService.userList(userForm, pageable);
        modelMap.addAttribute("userStatus",userForm.getDeleteFlag());
        modelMap.addAttribute("userList", tUsers);
        modelMap.addAttribute("totalNum", tUsers.size());
        modelMap.addAttribute("pageNum", pageable.getPageNumber());
        modelMap.addAttribute("pageSize", pageable.getPageSize());
        return "userManager/userContent";
    }

    @RequestMapping(value = "/addUser")
    String addUser() {
        return "userManager/addUser";
    }


    @RequestMapping("/saveUser")
    @ResponseBody
    Callable<OperateResponse> saveUser(TUser user){
        return () -> employeeService.saveUser(user);
    }

    @RequestMapping("/getAllCargoList")
    String getAllCargoList(){
        return "cargoManager/cargoList";
    }


    @RequestMapping("/addCargo")
    String addCargo(){
        return "cargoManager/addCargo";
    }

    @RequestMapping("/noticeList")
    String noticeList(ModelMap map){
        TNoticeExample example = new TNoticeExample();
        example.setOrderByClause("create_datetime desc");
        map.addAttribute("noticeList", noticeMapper.selectByExample(example));
        return "noticeManager/noticeManager";
    }


    @RequestMapping("/addNotice")
    String addNotice(){
        return "noticeManager/addNotic";
    }


    @RequestMapping("/makeOrder")
    String makeOrder(){
        return "orderManager/addOrder";
    }

    @RequestMapping("/orderList")
    String orderList(){
        return "orderManager/orderList";
    }

    @RequestMapping("/orderDetailList")
    String orderDetailList(){
        return "orderManager/orderDetailList";
    }

    @RequestMapping("/orderListEchart")
    String orderListEchart(){
        return "orderManager/orderListEchart";
    }

    @RequestMapping("/orderDetailEchart")
    String orderDetailEchart(){
        return "orderManager/orderDetailEchart";
    }

    @RequestMapping("/cargoDetail")
    String cargoDetail(ModelMap map, TCargo cargo, int isUpdate){
        map.addAttribute("cargo", cargo);
        map.addAttribute("isUpdate", isUpdate);
        return "cargoManager/cargoDetail";
    }

    @RequestMapping("/userDetail")
    String userDetai(ModelMap map, String userId){
        map.addAttribute("user", userMapper.selectByPrimaryKey(userId));
        return "userManager/userDetail";
    }

//    @RequestMapping("/uploadImg")
//    @ResponseBody
//    OperateResponse uploadImg(MultipartFile imgFile, HttpServletRequest request) throws FileNotFoundException {
//
//
//        //获取跟目录
////        if(!path.exists()) path = new File("");
////        System.out.println("path:"+path.getAbsolutePath());
////
//////如果上传目录为/static/images/upload/，则可以如下获取：
////        File upload = new File(path.getAbsolutePath(),"static/images/upload/");
////        if(!upload.exists()) upload.mkdirs();
////        System.out.println("upload url:"+upload.getAbsolutePath());
////在开发测试模式时，得到的地址为：{项目跟目录}/target/static/images/upload/
////在打包成jar正式发布时，得到的地址为：{发布jar包目录}/static/images/upload/
//        return OperateResponse.obtainSuccessResponse("/upload/"+originalFilename);
//    }

}
